<template>
	<view class="content">
		<view class="headContent" style="padding-top: 30rpx;">
			<form>
				<view class="cu-form-group margin-top">
					<view class="title"><text class="icon iconfont icon-user">&#xe600;</text></view>
					<input disabled="false" type="text" v-model="phone" name="input" style="text-align: left;"></input>
				</view>
				<view class="cu-form-group">
					<view class="title"><text class="icon iconfont icon-yanzhengma-copy"></text></view>
					<input type="number" maxlength="6" v-model="code" placeholder="请填写验证码" name="input" style="text-align: left;"></input>
					<button @click="getCodeNum()" type="primary">{{!codeTime?'获取验证码':codeTime+'s'}}</button>
				</view>
				<view class="cu-form-group">
					<view class="title"><text class="icon iconfont icon-mima"></text></view>
					<input :type="typeName1" maxlength="13" v-model="oldPwd" placeholder="请输入旧密码" name="input" class="input"></input>
					<image @tap="changePwdType1()" v-if="isShow" src="../../static/eyes.png" style="width: 50rpx; height: 50rpx;" mode="aspectFit"></image>
					<image @tap="changePwdType1()" v-else src="../../static/eyes2.png" style="width: 50rpx; height: 50rpx;" mode="aspectFit"></image>
				</view>
				<view class="cu-form-group">
					<view class="title"><text class="icon iconfont icon-mima"></text></view>
					<input :type="typeName2" maxlength="13" v-model="newPwd" placeholder="请输入新密码" name="input" class="input"></input>
					<image @tap="changePwdType2()" v-if="isShow2" src="../../static/eyes.png" style="width: 50rpx; height: 50rpx;"
					 mode="aspectFit"></image>
					<image @tap="changePwdType2()" v-else src="../../static/eyes2.png" style="width: 50rpx; height: 50rpx;" mode="aspectFit"></image>
				</view>
			</form>
		</view>

		<view class="submit">
			<button @tap="submit()">
				确认
			</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				number1: 0,
				number2: 0,
				isShow: false,
				isShow2: false,
				typeName1: "password",
				typeName2: "password",
				phone: '',
				code: '',
				oldPwd: '',
				newPwd: '',
				codeTime: 0,
				appid:""
			}
		},
		methods: {
			changePwdType1() {
				if (this.number1 === 0) {
					this.isShow = true
					this.number1++
					this.typeName1 = "text"
				} else {
					this.isShow = false
					this.number1--
					this.typeName1 = "password"
				}
			},

			changePwdType2() {
				if (this.number2 === 0) {
					this.isShow2 = true
					this.number2++
					this.typeName2 = "text"
				} else {
					this.isShow2 = false
					this.number2--
					this.typeName2 = "password"
				}
			},

			submit() {
				if (this.code.toString() === "") {
					uni.showToast({
						title: "请填写验证码",
						icon: "none"
					})
					return;
				} else if (this.newPwd.toString() === "") {
					uni.showToast({
						title: "请填写旧密码",
						icon: "none"
					})
					return;
				} else if (this.oldPwd.toString() === "") {
					uni.showToast({
						title: "请填写新密码",
						icon: "none"
					})
					return;
				} else if (this.oldPwd.toString() === this.newPwd.toString()) {
					uni.showToast({
						title: "新密码不能和旧密码相同",
						icon: "none"
					})
					return;
				} else {
					console.log(this.oldPwd.toString(), this.newPwd.toString(), this.phone, this.code)
					uni.request({
						url: this.confige.webUrl + "/updUserPasswordByUserId",
						method: "POST",
						header: {
							'X-Token': this.User.token
						},
						data: {
							mobile: this.phone,
							oldpassword: this.oldPwd.toString(),
							newpassword: this.newPwd.toString(),
							smsCode: this.code
						},
						success: (res) => {
							console.log(res)
							if (res.data.message == '修改密码成功，请妥善保管新密码') {
								uni.showModal({
									title: "提示",
									content: "修改密码成功，请妥善保管新密码",
									showCancel: false,
									success: (option) => {
										if (option.confirm) {
											this.User.close();
											this.User.isConnnect = false;
											uni.removeStorageSync('isConnnect'+this.User.userInfo.id);
											uni.removeStorageSync('token');
											this.User.token = "";
											uni.redirectTo({
												url: '../userLogin/userLogin'
											})
										}
									}
								})
							} else if(res.data.code==400) {
								uni.showModal({
									title: "提示",
									content: res.data.message,
									showCancel: false,
									success: (option) => {
										if (option.confirm) {
											return
										}
									}
								})
							}
						}
					})
				}
			},
			//获取验证码
			getCodeNum() {
				var pinf = plus.push.getClientInfo();
				console.log(pinf);
				this.appid = pinf.token;
				console.log(this.appid)
				if (this.codeTime > 0) {
					uni.showToast({
						title: '不能重复获取',
						icon: "none"
					});
					return;
				}
				this.codeTime = 60;
				let timer = setInterval(() => {
					this.codeTime--;
					if (this.codeTime < 1) {
						clearInterval(timer);
						this.codeTime = 0;
					}
				}, 1000)
				uni.request({
					url: this.confige.webUrl + "/code/sms",
					data: {
						mobile: this.phone,
						appId:this.appid
					},
					success: (res) => {
						console.log(res);
						uni.showToast({
							icon:"none",
							title:res.data.data,
							duration:2000
						})
					}
				})
			},
		},
		onReady() {
			this.phone = this.User.userInfo.phone
		}
	}
</script>

<style lang="less" scoped>
	@import "../../common/colorui/main.css";
	@import "../../common/colorui/icon.css";
	@import "../../common/global.less";

	page {
		width: 750rpx;
		height: 100%;
	}

	.content {
		width: 100%;
		height: 100%;
		background: #F6F6F6;
	}

	.headContent {
		width: 700rpx;
		margin-left: 25rpx;
	}

	.input {
		text-align: left;
		width: 100%;
		height: 100%;
		outline: medium;
		border: none;
		font-size: 30rpx;
		color: #555555;
		letter-spacing: 4rpx;
	}

	.cu-form-group>button {
		height: 80rpx;
		line-height: 80rpx;
		font-size: 26rpx;
		background: #4AC997;
		width: 200rpx;
	}

	.submit {
		width: 700rpx;
		margin-left: 25rpx;
		margin-top: 40rpx;
	}

	.submit>button {
		background: #4AC997;
		color: white;
		height: 90rpx;
		line-height: 90rpx;
		font-size: 32rpx;
	}
</style>
